/**
 *
 */
package com.hz.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import com.alibaba.fastjson.JSONObject;
import com.hz.model.User;
import com.hz.services.UserServices;
import com.hz.services.impl.UserServicesImpl;

/**
 * @author Administrator
 * @date 2018年6月20日 上午10:52:45
 * 
 */
@WebServlet("/user")
public class UserServlet extends HttpServlet {

	private UserServices userSerices = new UserServicesImpl();
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		String flag = req.getParameter("flag");
		if("add".equals(flag)) {
			add(req,resp);
		}else if("select".equals(flag)) {
			select(req,resp);
		}else if("mod".equals(flag)) {
			mod(req, resp);
		}else if("del".equals(flag)) {
			del(req, resp);
		}else {
			PrintWriter print = resp.getWriter();
			print.write("{\"result\":\"failure\"}");
		}
		
	}
	
	public void select(HttpServletRequest req, HttpServletResponse resp) throws IOException {
//		User user = getParameterFromRequest(req);
		User user = new User();
		user.setName(req.getParameter("name"));
		List<User> results = userSerices.query(user);
		JSONObject json = new JSONObject();
		json.put("data", results);
		
		PrintWriter print = resp.getWriter();
		print.write(json.toJSONString());
		
	}
	
	public void del(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String strids = req.getParameter("ids");
		if(strids != null) {
			if(strids != "" && strids.indexOf(",") != -1) {
				String ids[] = strids.split(",");
				for(String id : ids) {
					userSerices.del(Integer.parseInt(id));
				}
			}else {
				userSerices.del(Integer.parseInt(strids));
			}
		}
		
	}
	
	public void mod(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		User user = getParameterFromRequest(req);
		userSerices.mod(user);
		PrintWriter print = resp.getWriter();
		print.write("{\"result\":\"success\"}");
		
		
	}
	
	
	/**
	 * 新增用户的方法
	 * @param req
	 * @param resp
	 * @throws IOException 
	 */
	public void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		User user = getParameterFromRequest(req);
		//调用新增方法
		userSerices.add(user);
		PrintWriter writer = resp.getWriter();
		writer.write("welcom; success ");
		writer.write("{\"result\":\"success\"}");
		writer.flush();
	}
	
	private User getParameterFromRequest(HttpServletRequest req) {
		//获取所有参数的对象 map对象
		Map parameters = req.getParameterMap();	
		User user = new User();	
		//把参数的map对象 根据key与对象的属性的对应关系 映射到user实体对象中
		try {
			BeanUtils.populate(user, parameters);
		} catch (IllegalAccessException | InvocationTargetException e) {
			e.printStackTrace();
		}
		return user;
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		super.doPost(req, resp);
	}
	
	
	
}
